package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.Storyline;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.service.StorylineService;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.ParseJwtUtil;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;


@CrossOrigin
@RestController
@RequestMapping("storyline")
public class StorylineController {
    @Autowired
    StorylineService storylineService;

    @Autowired
    AdminUserService adminUserService;
    /**
     * 故事线添加
     * @param storyline
     * @return
     */
    @PostMapping("/admin/add")
    public R addStoryline(@RequestBody Storyline storyline){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
            storyline.setSpotId(spotId);
            if(null == storyline.getPartId()){
                return R.error("请选择故事id");
            }
            storylineService.addStoryline(storyline);
            return R.ok();
        }else if("0".equals(role)){
            if(null == storyline.getPartId()){
                return R.error("请选择故事id");
            }
            storylineService.addStoryline(storyline);
            return R.ok();
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 故事线修改
     * @param storyline
     * @return
     */
    @PostMapping("/admin/update")
    public R updateStoryline(@RequestBody Storyline storyline){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role) ){
            storylineService.addStoryline(storyline);
            return R.ok();
        }else if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
            if(spotId.equals(storyline.getSpotId())){
                storylineService.addStoryline(storyline);
                return R.ok();
            }else{
                return R.error(ResultCode.NO_ROLE_ERROR);
            }
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 删除
     * @param lineId
     * @return
     */
    @RequestMapping("/admin/delete")
    public R deleteStoryline(@RequestParam("lineId")Integer lineId){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role) ){
            storylineService.deleteStoryline(lineId);
            return R.ok();
        }else if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
                storylineService.deleteBySpotId(lineId,spotId);
                return R.ok();
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 故事线列表
     * @param page
     * @param limit
     * @param partId
     * @return
     */
    @GetMapping("/admin/list")
    public R getStoryline(@RequestParam("page")Integer page,
                          @RequestParam("limit")Integer limit,
                          @RequestParam("partId")Integer partId){
        String role = ParseJwtUtil.getRole();
        Pageable pageable = PageUtil.pagelimit(page, limit);
        if("0".equals(role) ){
            Page<Storyline> pages = storylineService.getListByPartId(partId,pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
            Page<Storyline> pages = storylineService.getListByPartIdAndSportId(partId,pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    //获取故事线
    @GetMapping("/frontent/info")
    public R getList(@RequestParam("lineId")Integer lineId){
        return storylineService.findById(lineId);
    }



    /**
     * 创建故事线二维码
     * @param lineId
     * @return
     */
    @GetMapping("/admin/createcode")
    public R createQrCode(@RequestParam("lineId")Integer lineId){
        return storylineService.createCode(lineId);
    }
}
